package com.njworkorder.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.DTO.InspectionCarAddDto;
import com.njworkorder.Entity.AdministrativeOrganization;
import com.njworkorder.Entity.InspectionCar;
import com.njworkorder.Service.InspectionCarService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.InspectionCarListVo;
import com.njworkorder.VO.InspectionCarStructureVo.InspectionCarSortVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "巡检车辆管理", description = "巡检车辆管理相关接口")
@RestController
@RequestMapping("/inspectionCar")
public class InspectionCarController {

    private final InspectionCarService inspectionCarService;
    public InspectionCarController(InspectionCarService inspectionCarService) {
        this.inspectionCarService = inspectionCarService;
    }

    @Operation(summary = "添加巡检车辆信息")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody InspectionCarAddDto inspectionCarAddDto) {
        InspectionCar inspectionCar = new InspectionCar();
        BeanUtils.copyProperties(inspectionCarAddDto, inspectionCar);

        boolean save = inspectionCarService.save(inspectionCar);
        if (save) {
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    @Operation(summary = "获取巡检车辆列表")
    @GetMapping("/getList")
    public Result<Page<InspectionCarListVo>> getList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize) {
        MPJLambdaWrapper<InspectionCar> mpjLambdaQueryWrapper = new MPJLambdaWrapper<InspectionCar>()
                .selectAll(InspectionCar.class)
                .leftJoin(AdministrativeOrganization.class, AdministrativeOrganization::getId, InspectionCar::getAoId,ext ->
                        ext.selectAs(AdministrativeOrganization::getName, InspectionCarListVo::getAoName))
                .apply("1=1");

        Page<InspectionCarListVo> inspectionCarListVoPage = inspectionCarService.selectJoinListPage(new Page<>(pageIndex, pageSize), InspectionCarListVo.class, mpjLambdaQueryWrapper);

        return ResultUtil.success(inspectionCarListVoPage);
    }

    @Operation(summary = "删除巡检车辆信息")
    @GetMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") String id) {
        boolean remove = inspectionCarService.removeById(id);
        if (remove) {
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @Operation(summary = "根据id更新巡检车辆信息")
    @PostMapping("/updateById/{id}")
    public Result<String> updateById(@RequestBody InspectionCarAddDto inspectionCarAddDto,@PathVariable String id) {
        InspectionCar inspectionCar = new InspectionCar();
        BeanUtils.copyProperties(inspectionCarAddDto, inspectionCar);
        inspectionCar.setId(Integer.parseInt(id));

        boolean update = inspectionCarService.updateById(inspectionCar);
        if (update) {
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.fail("更新失败");
    }

    @Operation(summary = "根据id获取巡检车辆信息")
    @GetMapping("/getInfoById")
    public Result<InspectionCarListVo> getInfoById(@RequestParam("id") String id) {

        MPJLambdaWrapper<InspectionCar> mpjLambdaQueryWrapper = new MPJLambdaWrapper<InspectionCar>()
                .selectAll(InspectionCar.class)
                .leftJoin(AdministrativeOrganization.class, AdministrativeOrganization::getId, InspectionCar::getAoId,ext ->
                        ext.selectAs(AdministrativeOrganization::getName, InspectionCarListVo::getAoName))
                .eq(InspectionCar::getId, id);

        InspectionCarListVo inspectionCarListVo = inspectionCarService.selectJoinOne(InspectionCarListVo.class, mpjLambdaQueryWrapper);
        return ResultUtil.success(inspectionCarListVo);
    }


    @Operation(summary = "调度管理车辆隶属结构")
    @GetMapping("/getInfoStructure")
    public Result<List<Map<String,Object>>> getInfoStructure(){
        MPJLambdaWrapper<InspectionCar> mpjLambdaQueryWrapper = new MPJLambdaWrapper<InspectionCar>()
                .select(InspectionCar::getId, InspectionCar::getAoId,InspectionCar::getCarNum,InspectionCar::getCarNumColor,InspectionCar::getAoStructure,
                        InspectionCar::getCommunicationCardNum,InspectionCar::getRealCardNum,InspectionCar::getProductType,
                        InspectionCar::getCarPurpose,InspectionCar::getCarStatus,InspectionCar::getTerminalType)
                .apply("1=1").eq(InspectionCar::getAoId,"高邮养护中心");
        List<InspectionCarSortVo> inspectionCarSortVos = inspectionCarService.selectJoinList(InspectionCarSortVo.class, mpjLambdaQueryWrapper);

        Map<String, List<InspectionCarSortVo>> collect = inspectionCarSortVos.stream().collect(Collectors.groupingBy(InspectionCarSortVo::getAoId));

        List<Map<String,Object>> first = new ArrayList<>();//first
        List<Map<String,Object>> second = new ArrayList<>();//second

        collect.forEach( (key, value) -> {
            Map<String,Object> map = new HashMap<>();
            map.put("aoId",key);

            Map<String, List<InspectionCarSortVo>> collect1 = value.stream().collect(Collectors.groupingBy(InspectionCarSortVo::getCarPurpose));

            collect1.forEach((key1, value1) -> {
                Map<String,Object> map1 = new HashMap<>();
                map1.put("carPurpose",key1);
                map1.put("carInfo",value1);
                second.add(map1);
                map.put("carPInfo",second);
            });
            first.add(map);
        });

        return ResultUtil.success(first);
    }



}
